package com.example.tellwin.mine.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.tellwin.R;
import com.example.tellwin.utils.GlideUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : LinZePeng
 * @date : 2020/11/13
 * @Describe ：
 **/
public class OrderPhotoAdapter extends RecyclerView.Adapter<OrderPhotoAdapter.OrderPhotoViewHolder> {

    Context context;
    List<String>photoPaths = new ArrayList<>();
    OnItemClickListener onItemClickListener;

    public void setPhotoPaths(List<String> photoPaths) {
        this.photoPaths = photoPaths;
        notifyDataSetChanged();
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @NonNull
    @Override
    public OrderPhotoAdapter.OrderPhotoViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        context = parent.getContext();
        return new OrderPhotoViewHolder(LayoutInflater.from(context).inflate(R.layout.item_order_photo ,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull OrderPhotoAdapter.OrderPhotoViewHolder holder, int position) {
        GlideUtils.loadRoundImage(context , photoPaths.get(position) , holder.photoIv , context.getResources().getDimensionPixelOffset(R.dimen.dp_4));
        holder.photoIv.setOnClickListener(view -> {
            if(onItemClickListener!=null){
                onItemClickListener.onItemClick(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return photoPaths.size();
    }

    class OrderPhotoViewHolder extends RecyclerView.ViewHolder{

        ImageView photoIv;

        public OrderPhotoViewHolder(@NonNull View itemView) {
            super(itemView);
            photoIv = itemView.findViewById(R.id.photo_iv);
        }
    }

    public interface OnItemClickListener{
        void onItemClick(int position);
    }
}
